<template>
  <div>
    <nav-bar title="违章新增"></nav-bar>
    <div class="maxinBox">
       <van-form>
         <van-cell required is-link title="姓名" :value="username" @click="showBuilder = true" />
         <van-action-sheet v-model="showBuilder" :actions="builderList" @select="onSelectBuilder" />
         <van-cell title="班组" :value="teamName" />
         <van-cell title="工种" :value="workTypeName" />
         <van-field
           v-model="mobile"
           name="手机号码"
           label="手机号码"
           disabled
           placeholder="请输入手机号码"
         />
         <van-cell class="marginTop5" required title="违章日期" @click="violation" is-link :value="form.illegalDate" />
         <van-popup v-model="showViolation" position="bottom" style="height: 40%">
           <van-datetime-picker
             v-model="currentDate"
             @confirm="violationConfirm"
             @cancel="showViolation = false"
             type="date"
             title="请选择违章日期"
             :min-date="violateMinDate"
             :max-date="violateMaxDate"
           />
         </van-popup>
         <van-cell required is-link title="违章类别" :value="violate" @click="showViolate = true" />
         <van-action-sheet v-model="showViolate" :actions="violateList" @select="onSelectViolate" />
         <van-field required name="uploader" label="附件上传">
           <template #input>
             <van-uploader multiple :max-count="1" v-model="imagesList" :after-read="onUploader" :before-delete="delUpload" />
           </template>
         </van-field>
         <div style="margin: 16px;">
           <van-button round block type="info" @click="onSubmit">
             提交
           </van-button>
         </div>
       </van-form>
    </div>
  </div>
</template>

<script>
  import moment from 'moment'
  import dashbordAPI from '../../../api/dashbordAPI.js'
  import violationAdd from '../../../api/workAPI/violationAdd.js'
  export default {
    data() {
      return {
        imagesList: [],
        showViolate: false,
        showViolation: false,
        showBuilder: false,
        form: {
          behavior: '',
          builderId: '',
          illegalDate: '',
          attachmentList: [],
          projectId: localStorage.getItem('projectId')
        },
        teamName: '',
        workTypeName: '',
        username: '',
        mobile: '',
        currentDate: new Date(),
        violateMinDate: new Date(2000, 0, 1),
        violateMaxDate: moment(new Date()).add(1,'days')._d,
        violate: '',
        violateList: [],
        builderList: []
      }
    },
    methods: {
      // 查询施工人员列表
      getBuilderList() {
        let data = {
          pageSize: 200,
          pageNum: 1,
          status: 2,
          passExam: 1,
          isBlack: 0
        }
        let projectId = localStorage.getItem('projectId')
        violationAdd.getBuilderList(projectId, data).then(res => {
          this.builderList = res.map(item => {
            return {
              name: item.name,
              value: item.id,
              data: item
            }
          })
        })
      },
      // 上传图片
      onUploader(file) {
        file.status = 'uploading'
        file.message = '上传中'
        dashbordAPI.uploadAvatar(file.file).then(res => {
          this.form.attachmentList.push(res.fileUrl)
          file.status = ''
          file.message = ''
        }).finally(() => {
          file.status = ''
          file.message = ''
        })
      },
      // 删除上传图片
      delUpload(file) {
        this.imagesList.forEach(item => {
          if (item.file.name === file.file.name) {
            let index = this.imagesList.indexOf(item);
            this.imagesList.splice(index, 1)
            this.form.attachmentList.splice(index, 1)
          }
        })
      },
      // 违章类别
      getViolate() {
        dashbordAPI.getViolate().then(res => {
          this.violateList = res.data.map(item => {
            return {
              name: item.dictLabel,
              value: item.dictCode,
            }
          })
        })
      },
      // 选择施工人员
      onSelectBuilder(item) {
        this.showBuilder = false;
        this.form.builderId = item.value
        this.username = item.name
        this.mobile = item.data.mobile
        this.workTypeName = item.data.workTypeName
        this.teamName = item.data.teamName
      },
      // 选择违章类型
      onSelectViolate(item) {
        this.showViolate = false;
        this.form.behavior = item.value
        this.violate = item.name
      },
      // 选择证件有效期
      violation() {
        this.showViolation = true
      },
      // 证件有效期日期选择确定按钮
      violationConfirm(val) {
       this.form.illegalDate = moment(val).format('YYYY-MM-DD')
       this.showViolation = false
      },
      // 提交
      onSubmit() {
        if (this.form.illegalDate === '') {
          this.$toast.fail('请输入违章时间')
          return
        }
        if (this.form.behavior === '') {
          this.$toast.fail('请选择违章类别')
          return
        }
        if (this.form.builderId === '') {
          this.$toast.fail('请输入姓名')
          return
        }
        if (this.imagesList.length === 0) {
          this.$toast.fail('请上传图片')
          return
        }
        if (this.form.attachmentList.length === 0) {
          this.$toast.fail('正在上传服务器... ')
          return
        }
        violationAdd.addViolation(this.form).then(res => {
          this.$toast('新增成功')
          this.$router.push({
            name: 'IllegalQuery'
          })
        })
      }
    },
    mounted() {
      this.getViolate()
      this.getBuilderList()
    }
  }
</script>
<style scoped lang="scss">
/deep/ .van-field--disabled .van-field__label{
  color: #646566;
}
/deep/ .van-field__control:disabled{
  color: #c8c9cc;
}
</style>
